//
//  YSPCustomButton.swift
//  Weibo Demo
//
//  Created by 朴裕识 on 15/12/20.
//  Copyright © 2015年 Apple. All rights reserved.
//

import UIKit

class YSPCustomButton: UIButton {
    
    init(imageName: String, title: String) {
        super.init(frame: CGRectZero)
        setImage(UIImage(named: imageName), forState: UIControlState.Normal)
        setTitle(title, forState: UIControlState.Normal)
        setTitleColor(UIColor.blackColor(), forState: UIControlState.Normal)
        sizeToFit()
        addTarget(self, action: "doWhenClicked", forControlEvents: UIControlEvents.TouchUpInside)
    }
    
    func doWhenClicked() {
        selected = !selected
        UIView.animateWithDuration(0.25) { () -> Void in
            self.imageView!.transform = self.selected ? CGAffineTransformMakeRotation(CGFloat(M_PI - 0.001))
            : CGAffineTransformIdentity
        }
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override func layoutSubviews() {
        super.layoutSubviews()
        titleLabel?.frame.origin.x = 0
        imageView?.frame.origin.x = (titleLabel?.frame.size.width)! + 3
        
    }
    

}
